from PyQt5.QtWidgets import QWidget, QSizePolicy

from widget.node_content_widget_ui import Ui_NodeContentWidget


class NodeContentWidget(QWidget, Ui_NodeContentWidget):
    """
    承载所有列表的子类
    """
    def __init__(self, parent):
        super(NodeContentWidget, self).__init__()
        self._back_state = False
        self.setupUi(self)
        self.back_state = self._back_state
        self._parent: NodeContentWidget = parent
        self._signal_init()
        self._custom_init()

    def _signal_init(self):
        self.back_pb.clicked.connect(self.on_back_button_clicked)

    def _custom_init(self):
        self.back_widget.hide()
        if self._parent is None:
            self.parent_scroll_area.show()
        else:
            self.parent_scroll_area.hide()
        self.child_scroll_area.hide()
        # 测试用
        # self.child_scroll_area.setStyleSheet("background-color:red;")
        # self.parent_scroll_area.setStyleSheet("background-color:yellow;")

    @property
    def title_name(self):
        return self.list_name_lb.text()

    @title_name.setter
    def title_name(self, title: str):
        self.list_name_lb.setText(title)

    @property
    def back_state(self) -> bool:
        return self._back_state

    @back_state.setter
    def back_state(self, state: bool):
        self._back_state = state
        self.back_widget.setHidden(not state)

    def on_back_button_clicked(self):
        self._parent.parent_scroll_area.show()
        self.set_size_maximum(self._parent.parent_scroll_contents)
        self._parent.child_scroll_area.hide()
        self.set_size_minimum(self._parent.child_scroll_contents)
        if self._parent._parent is not None:
            self._parent.back_widget.show()
        self.parent_scroll_area.hide()
        self.set_size_minimum(self.parent_scroll_contents)
        self.child_scroll_area.hide()
        self.set_size_minimum(self.child_scroll_contents)
        self.back_widget.hide()
        print("{}:show parent, hide child, hide back pb".format(self.objectName()))

    def insert_item_to_parent_area(self, w: QWidget):
        self.parent_item_vl.addWidget(w)

    def insert_item_to_child_area(self, w: QWidget):
        self.child_item_vl.addWidget(w)

    def item_double_clicked(self):
        self._parent.parent_scroll_area.hide()
        self.set_size_minimum(self._parent.parent_scroll_contents)
        self._parent.child_scroll_area.show()
        self.set_size_maximum(self._parent.child_scroll_contents)
        self._parent.back_widget.hide()
        self.parent_scroll_area.show()
        self.set_size_maximum(self.parent_scroll_contents)
        self.child_scroll_area.hide()
        self.set_size_minimum(self.child_scroll_contents)
        self.back_widget.show()
        print("{}:hide parent, show child, show back pb".format(self.objectName()))

    def set_size_maximum(self, widget):
        size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
        size_policy.setHorizontalStretch(0)
        size_policy.setVerticalStretch(0)
        size_policy.setHeightForWidth(widget.sizePolicy().hasHeightForWidth())

    def set_size_minimum(self, widget):
        size_policy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Maximum)
        size_policy.setHorizontalStretch(0)
        size_policy.setVerticalStretch(0)
        size_policy.setHeightForWidth(widget.sizePolicy().hasHeightForWidth())
